package com.toubutu.audio.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.toubutu.audio.admin.dto.AdminVideoTagQueryDto;
import com.toubutu.audio.admin.dto.VideoTagDto;
import com.toubutu.audio.entity.VideoTag;
import com.toubutu.audio.mapper.VideoTagMapper;
import com.toubutu.audio.service.VideoTagService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.toubutu.common.BusinessException;
import com.toubutu.common.PageUtils;
import com.toubutu.common.StatusCodeEnum;
import com.toubutu.constants.BaseConstants;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 戒躁
 * @since 2024-01-10
 */
@Service
public class VideoTagServiceImpl extends ServiceImpl<VideoTagMapper, VideoTag> implements VideoTagService {

    @Override
    public PageUtils<VideoTag> pageQueryVideoTage(AdminVideoTagQueryDto videoTagQueryDto) {
        Page<VideoTag> page = videoTagQueryDto.getPage();
        LambdaQueryWrapper<VideoTag> wrapper = videoTagQueryDto.getWrapper();
        page=page(page,wrapper);
        return new PageUtils<>(page.getRecords(),page);
    }

    @Override
    public List<VideoTag> getAllTags() {
        LambdaQueryWrapper<VideoTag>  queryWrapper= new LambdaQueryWrapper<>();
        queryWrapper.eq(VideoTag::getIsDelete, BaseConstants.UN_DELETED);
        return list(queryWrapper);
    }

    @Override
    public VideoTag getTagInfo(Integer tagId) {
        LambdaQueryWrapper<VideoTag> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(VideoTag::getTagId,tagId).eq(VideoTag::getIsDelete,BaseConstants.UN_DELETED);
        VideoTag one = getOne(queryWrapper);
        if (one==null){
            throw  new BusinessException(StatusCodeEnum.NOT_FOUND);
        }
        return one;
    }

    @Override
    public void addTag(VideoTagDto videoTagDto) {
        checkNameIsExist(videoTagDto);
        VideoTag videoTag = videoTagDto.toPo();
        save(videoTag);
    }

    @Override
    public void deleteTagById(Integer tagId) {
        LambdaUpdateWrapper<VideoTag> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(VideoTag::getTagId,tagId).set(VideoTag::getIsDelete,BaseConstants.DELETED);
        update(updateWrapper);
    }

    @Override
    public void updateTag(VideoTagDto videoTagDto) {
        checkNameIsExist(videoTagDto);
        LambdaUpdateWrapper<VideoTag> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(VideoTag::getTagId,videoTagDto.getTagId())
                .set(VideoTag::getTagName,videoTagDto.getTagName());
        update(updateWrapper);
    }

    private void checkNameIsExist(VideoTagDto videoTagDto){
        LambdaQueryWrapper<VideoTag> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(VideoTag::getTagName,videoTagDto.getTagName());
        VideoTag one = getOne(queryWrapper);
        if (one!=null){
            throw new BusinessException(StatusCodeEnum.USER_HANDLE.getCode(),"标签名称已存在!");
        }
    }
}
